define(function(require, exports, module) {
	"use strict";
	require("mui");
	require("jquery");
	require("common");
	var template = require("template");
	var abpUse = require("../abp.js");
	var muiTips = require("muiTips");
	var globalInit = require("globalInit"); //共用js
	var abp = abpUse.abpstart;
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');
	//钉钉

	function getRightFun() {
		var popoverMore = $("header > a").attr("href");
		mui(popoverMore).popover('toggle');
	}

	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityWH_ALLOCATE = authorityInit.getJurisdiction(authorityData, sourceType.WH_ALLOCATE); //库存调拨权限判断
	if(authorityWH_ALLOCATE.indexOf(3) === -1) {
		$(".mui-content").append("<div class='tipsConInit'></div>");
		muiTips.alertTips(' ', '您没有操作订单的权限，请联系管理员开通吧！', 2, 3, "default");
		$(".more").remove();
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "更多", "default");
	} else {
		$(".more").show(); //按钮显示
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(true, "更多", getRightFun);
	}

	//接收审批中心的数据
	var SourceId = sessionStorage.getItem('SourceId');
	var AuditId = sessionStorage.getItem('AuditId');
	var Audit_detailId = sessionStorage.getItem('Audit_detailId');
	var SourceType = sessionStorage.getItem('SourceType');

	//接受调拨列表页面的数据

	//获取当前单据Id
	var AllocateID = sessionStorage.getItem('allocateID');

	function historyAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, true, $('#notes').text());
	}

	function historyNoAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, false, $('#notes').text());
	}

	function setResetDraft() {
		ResetDraft(AllocateID);
	}

	function setDelete() {
		Delete(AllocateID);
	}
	//“更多”按钮点击事件
	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		switch($(this).text()) {
			case '重置草稿':
				muiTips.confirmTips(' ', '您确定要重置吗！', 2, 1, setResetDraft);
				break;
			case '通过':
				muiTips.alertTips(' ', '恭喜您已通过！', 1, 1, historyAudit);
				break;
			case '驳回':
				muiTips.alertTips(' ', '驳回成功！', 1, 1, historyNoAudit);
				break;
			case '编辑':
				//sessionStorage.setItem('allocateID',id);
				document.location.href = "../newAllotStock/newAllotStockOrder.html?dd_nav_bgcolor=FFFF943E";
				break;
			case '删除':
				muiTips.confirmTips(' ', '您确定要删除吗！', 2, 1, setDelete);
				break;
		}
	});

	//获取审批历史数据
	GetHisAuditRecord();

	//绑定表单数据  
	BindFormData();

	//重置草稿方法
	function ResetDraft(id) {

		if(!id || typeof(id) == 'undefined') {
			return;
		}

		var option = {};
		var dataModel = {
			"id": id,
			"serialNo": ""
		};
		abp.services.app.whAllocateList.resetDraft(dataModel, option).
		done(function(data) {
			if(data) {
				muiTips.alertTips(' ', '重置成功', 1, 1, function() {
					document.location.href = "../../src/allocationStock/index.html?dd_nav_bgcolor=FFFF943E";
				});

			}
			ClearSession();
			if(data.success) {

			} else {
				//console.log(data.error.message)   		
			}
		}).fail(function(data) {
			muiTips.alertAutoma(' ', data.message, 2, "default");
			//				mui.alert('<ul class="mui-row pop-tabmain"><li class="mui-col-xs-6 mui-col-sm-6"style="text-align: right;padding-right: 20px;"><a class="yuanico"><span>×</span></a></li><li class="mui-col-xs-6 mui-col-sm-6" style="text-align: left;line-height: 46px;">' + data.message + '</li></ul>', ' ', function() {}, 'div');
		});
	}

	//绑定表单数据方法 	
	function BindFormData() {

		if(!AllocateID || typeof(AllocateID) == 'undefined') {
			return;
		}

		var option = {};
		var dataModel = {
			"id": AllocateID
		};
		abp.services.app.whAllocateList.getModel(dataModel, option).
		done(function(data) {
			var allocateData = JSON.stringify(data);
			sessionStorage.setItem('allotData', allocateData);

			//单据详情绑定 
			$('#statusName').text(data.statusName);
			$('#statusName').attr("data-statusId", data.statusId);
			$('#serialNo').text(data.serialNO);
			$('#serialDate').text(data.serialDate.substring(0, 10));
			$('#inWhName').text(data.inWhName);
			$('#outWhName').text(data.outWhName);
			$('#operatorName').text(data.operatorName);
			$('#dataCreated').text(data.dataCreated.substring(0, 10));
			$('#remark').text(data.notes);
			var statusIdColor = require("colorOrder");
			statusIdColor("#statusName");
			//货品数据明细绑定
			var data1 = {};
			data1.list = data.whAllocateDetail;
			var html = template('allocateDetList', data1);
			$("#listAllocateDet").html(html);

			//审批历史数据绑定

			if(data.success) {

			} else {
				//console.log(data.error.message)

			}
		}).fail(function(data) {
			muiTips.alertAutoma(' ', data.message, 2, "default");
			//mui.alert('<ul class="mui-row pop-tabmain"><li class="mui-col-xs-6 mui-col-sm-6"style="text-align: right;padding-right: 20px;"><a class="yuanico"><span>×</span></a></li><li class="mui-col-xs-6 mui-col-sm-6" style="text-align: left;line-height: 46px;">' + data.message + '</li></ul>', ' ', function() {}, 'div');
		});

	}

	//获取审批历史数据方法
	function GetHisAuditRecord() {

		var option = {};
		var dataModel = {};
		if(sessionStorage.getItem('SourceId') && sessionStorage.getItem('AuditId') && sessionStorage.getItem('Audit_detailId') && sessionStorage.getItem('SourceType')) {
			//审批中心入口
			dataModel = {
				"id": SourceId
			};
			//初始化页面显示
			$('header > a').attr('href', '#eForMore');

			//调用审批历史数据
			setTimeout(function() {
				eHistory.historyCon('sHistoryList', '#historyList', SourceType, SourceId);
			}, 1000);

			//返回清除审批中心订单缓存
			mui('body').on('tap', 'header > button', function() {
				sessionStorage.removeItem('SourceId');
				sessionStorage.removeItem('AuditId');
				sessionStorage.removeItem('Audit_detailId');
				sessionStorage.removeItem('SourceType');
			});
		} else {
			//非审批中心入口
			dataModel = {
				"id": AllocateID
			};
			var allocateStastusId = sessionStorage.getItem("allocateStastusId");

			if(allocateStastusId == "6" || allocateStastusId == "7" || allocateStastusId == "8") {
				$('header > a').attr('href', '#resetDraftforMore');
			} else {
				$('header > a').attr('href', '#editForMore');
			}
		}

	}

	//删除单个单据方法
	function Delete(id) {
		if(!id || typeof(id) == 'undefined') {
			return;
		}

		var option = {};
		var dataModel = {
			"id": id,
			"serialNo": ""
		};
		abp.services.app.whAllocateList.delete(dataModel, option).
		done(function(data) {
			if(data) {
				muiTips.alertTips(' ', '删除成功', 1, 1, function() {
					document.location.href = "../../src/allocationStock/index.html?dd_nav_bgcolor=FFFF943E";
				});
			}
			ClearSession();
			sessionStorage.removeItem("allocateID");
			if(data.success) {

			} else {
				//console.log(data.error.message)   		
			}
		}).fail(function(data) {
			muiTips.alertAutoma(' ', data.message, 2, "default");
			//mui.alert('<ul class="mui-row pop-tabmain"><li class="mui-col-xs-6 mui-col-sm-6"style="text-align: right;padding-right: 20px;"><a class="yuanico"><span>×</span></a></li><li class="mui-col-xs-6 mui-col-sm-6" style="text-align: left;line-height: 46px;">' + data.message + '</li></ul>', ' ', function() {}, 'div');
		});
	}

	//返回按钮点击事件
	mui("body").on("tap", "header>button", function() {
		ClearSession();
		document.location.href = "../../src/allocationStock/index.html";
	});

	if(sessionStorage.getItem("allocateID")) {
		dingTalkNav.LeftNavBar(ClearSessionDD);
		//ad
		dingTalkNav.leftAndroid(ClearSessionDD);
	}
	//清楚缓存公调方法	
	//钉钉
	function ClearSessionDD() {
		//sessionStorage.removeItem("allocateID");
		sessionStorage.removeItem("outLibrary");
		sessionStorage.removeItem("inLibrary");
		sessionStorage.removeItem("chioceStaff");
		sessionStorage.removeItem("allotData");
		sessionStorage.removeItem("clientId");
		sessionStorage.removeItem("goodsInfoList");
		sessionStorage.removeItem("statusType");
		sessionStorage.removeItem("localstatusType");
		sessionStorage.removeItem("allocateID");
		sessionStorage.removeItem("allocateStastusId");
		document.location.href = "../../src/allocationStock/index.html?dd_nav_bgcolor=FFFF943E";
	}
	//清楚缓存公调方法	
	function ClearSession() {
		//sessionStorage.removeItem("allocateID");
		sessionStorage.removeItem("allocateID");
		sessionStorage.removeItem("allocateStastusId");
		sessionStorage.removeItem("outLibrary");
		sessionStorage.removeItem("inLibrary");
		sessionStorage.removeItem("chioceStaff");
		sessionStorage.removeItem("allotData");
		sessionStorage.removeItem("clientId");
		sessionStorage.removeItem("goodsInfoList");
		sessionStorage.removeItem("statusType");
		sessionStorage.removeItem("localstatusType");
	}

	globalInit.eHistoryInit("#historyList"); //审批历史数据提示
});